package cn.tiancaifan.micro.service;

import cn.tiancaifan.micro.entity.Hr;
import cn.tiancaifan.micro.entity.SystemResults;
import cn.tiancaifan.micro.mapper.HrMapper;
import cn.tiancaifan.micro.utils.HrUtils;
import cn.tiancaifan.micro.utils.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HrService implements UserDetailsService {

    @Autowired
    HrMapper hrMapper;
    @Override
    @Logger(value = "用户登录",login = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Hr hr = hrMapper.loadUserByUsername(username);
        if (hr ==null){
            throw new  UsernameNotFoundException("用户不存在!!!");
        }
        hr.setRoles(hrMapper.getHrRolesByid(hr.getId()));
        return hr;
    }


    public List<Hr> getAllHr() {
        return hrMapper.getAllHr(HrUtils.getCurrentHr().getId());
    }

    public SystemResults deleteHrByID(Integer id) {
        int i = hrMapper.deleteHrByID(id);
        if (i!=0){
            return SystemResults.ok("删除成功");
        }else {
            return SystemResults.error("删除失败！");
        }
    }
}
